<template>
<div class="content-center">
  <!-- 页面标签/页面tab -->
  <div class="content-cotainer border-box">
    <!-- 面包屑 -->
    <div class="content-breadcrumb">
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item  class="home-font">审计作业</el-breadcrumb-item>
        <el-breadcrumb-item>{{labelname}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 页面内容-->
    <div class="audit-content">
      <div class="centent-title">
        <span>底稿列表</span>
      </div>
      <!-- 按钮查询条件 -->
      <el-form :model="projectForm" label-width="100px" size="small" style="width:97%; margin:0 auto">
        <el-row style="margin-top:10px">
          <el-col :span="8">
            <el-form-item label="资料名称：">
              <el-input v-model="projectForm.docNm" size="small" placeholder="请输入资料名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上传人：">
              <el-select v-model="projectForm.auditorId" size="small" placeholder="请选择上传人">
                <el-option label="王鑫" value="wx"></el-option>
                <el-option label="邹阳" value="zy"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态：">
              <el-select v-model="projectForm.lcsCd" size="small" placeholder="请选择状态">
                <el-option label="正常" value="0"></el-option>
                <el-option label="废弃" value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="编制日期：">
          <div class="block">
            <el-date-picker v-model="projectForm.readTm"
              type="daterange" value-format="yyyy-MM-dd"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions">
            </el-date-picker>
            <el-button style="margin-left: 30px" type="primary" icon="el-icon-search" size="small" @click="searchList()">查询</el-button>
            <el-button style="margin-left: 30px" type="primary" icon="el-icon-refresh-left" size="small" @click="reset()">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
      <el-row  style="margin-bottom:10px">
        <el-button style="margin-left: 30px" type="primary" icon="el-icon-share" size="small" @click="share">共享</el-button>
        <el-button style="margin-left: 20px" type="primary" icon="el-icon-delete" size="small" :disabled="disdelete" @click="delData">{{buttonNm}}</el-button>
        <!-- <el-button style="margin-left: 20px" type="primary" icon="el-icon-refresh-right" size="small" @click="recoverData">恢复</el-button> -->
        <el-button style="margin-left: 20px" type="primary" size="small" @click="importData">上传资料</el-button>
      </el-row>
      <!-- 表格 -->
      <div class="inner-table" style="width:95%; margin:0 auto">
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          stripe
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50"></el-table-column>
          <el-table-column label="序号" width="50" align="center" type="index"></el-table-column>
          <el-table-column prop="docNm" label="资料名称" align="center"></el-table-column>
          <el-table-column prop="auditorNm" label="上传人" align="center"></el-table-column>
          <el-table-column prop="readTm" label="编制日期" :formatter="dateFormat" align="center" ></el-table-column>
          <el-table-column prop="lcsCd" label="状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.lcsCd === '0'">正常</span>
              <span v-else-if="scope.row.lcsCd === '4'">废弃</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="text-align: center;margin-top:10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length">
        </el-pagination>
      </div>
    </div>
    <!-- 共享弹框 -->
    <el-dialog title="选择共享的人" :visible.sync="dialogVisible" center @opened="opened">
      <share :data="gxData" @cancelshare="cancelshare" @sureshare="sureshare" opened></share>
    </el-dialog>
    <!-- 系统引入资料 -->
    <el-dialog title="上传资料" :visible.sync="importDialog" center @close="closeDialog">
      <el-upload action="https://jsonplaceholder.typicode.com/posts/"
        :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove"
        multiple :limit="5" :on-exceed="handleExceed" :file-list="fileList" :auto-upload="false">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button style="margin-left: 20px;" size="small" type="success" @click="submitUpload">上传</el-button>
        <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
      </el-upload>
    </el-dialog>
  </div>
</div>
</template>
<script>
  import axios from 'axios'
  import share from "@/components/Share"
  export default {
    components: {share},
  	data() {
      return {
        labelname: "",
        buttonNm: '废弃',
        disdelete: false,
        dialogVisible:false,
        importDialog:false,
        multipleSelection: [],
        gxData: [],
        fileList: [],
        projectForm: {
          docNm:"",
          auditorId:"",
          readTm:"",
          lcsCd:"",
        },
        currentPage:1, //初始页
        pagesize:10,   //每页的数据
        tableData: [],
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      }
    },
    computed:{
      labelname: function () {
        return this.$route.query.label
      }
    },
    // watch:{
    //   labelname: function (labelname) {this.labelname = this.$route.query.label}
    // },
    mounted() {
      //console.log('000')
      //this.projectForm.auditPrjId="00000001"
      //console.log(this.$route.query.label)
      this.labelname = this.$route.query.label
      this.searchList()
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val;
        if (val && Array.isArray(val) && val.length > 0) {
          if (val.every(item =>item.lcsCd === '0')) {
            this.buttonNm = '废弃'
            this.disdelete = false
            return false
          } else if (val.every(item =>item.lcsCd === '4')) {
            this.buttonNm = '启用'
            this.disdelete = false
            return false
          } else if (val.some(item =>item.lcsCd === '0') && val.some(item =>item.lcsCd === '4')) {
            this.disdelete = true
            return false
          }
        } else {
          this.disdelete = true
          return false
        }
      },
      handleSizeChange: function (size) {
        this.pagesize = size;//每页下拉显示数据
      },
      handleCurrentChange: function(currentPage){
        this.currentPage = currentPage;//点击第几页
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        //return this.$confirm(`确定移除 ${ file.name }？`);
      },
      submitUpload() {
        this.$refs.upload.submit();
      },
      //时间格式化
      dateFormat(row, column, cellValue, index){
        const daterc = row[column.property]
        if(daterc!=null){
          return  this.$moment(daterc).format('YYYY-MM-DD')
        }else{
          return "";
        }
      },
      searchList(){
        let nodeId = this.$route.query.nodeId
        let readTm = this.projectForm.readTm
        let entity = {
          //nodeId:nodeId,
          docNm:this.projectForm.docNm,
          auditorId:this.projectForm.auditorId,
          readTmStart:readTm[0],
          readTmEnd:readTm[1],
          lcsCd:this.projectForm.lcsCd
        }
        let params = {entity:entity,pageNum:this.currentPage,pageSize:this.pagesize}
        console.log(params)
        axios.post('http://128.16.180.47:7111/docFile/selectAuditOperations',params).then(response => {
          this.tableData = response.data.list;
        }).catch((error) => {
          console.log(error)
        })
      },
      share(){
        if (this.multipleSelection && Array.isArray(this.multipleSelection) && this.multipleSelection.length == 1) {
          this.dialogVisible = true
        } else if(this.multipleSelection && Array.isArray(this.multipleSelection) && this.multipleSelection.length == 0){
          this.$message.warning('未勾选需要共享的数据!')
        }else{
          this.$message.warning('只能选一条需要共享的数据!')
        }
      },
      // 确认分享
      sureshare (gxData) {
        console.log('确认分享table' + gxData)
        if (gxData && gxData.length > 0) {
          let info = {
            userId:"1001",
            auditGrpId:"10000001",
            auditPrjId:"00000001",
            unknownFileId:"abc12345",
            unknownFileType:"10",
            auditorIds:[]
          }
          gxData.forEach(item => {
            if (item.checkflag && item.checkflag === true) {
              info.auditorIds.push(
                item.auditorId
              )
            }
          })
          axios.post('http://128.16.180.47:7111/fileShare/modifyShareData', info).then((response) => {
            console.log(response)
            if (response && response.data.success) {
              this.$message.success('分享成功')
            }else{
              this.$message.error('分享失败')
            }
          }).catch((response) => {
            console.log(response)
          })
          this.dialogVisible = false
        }
      },
      // 取消分享
      cancelshare () {
        this.dialogVisible = false
      },
      opened() {
        this.getChildVueComponentAttr(this, 'opened')
        this.$emit('opened')
      },
      getChildVueComponentAttr(vm, attr) {
        if (vm == null || attr == null || attr === '') {
          return null
        }
        if (vm.$attrs.hasOwnProperty(attr)) {
          var fn = vm.$attrs[attr]
          if (fn === '') {
            fn = attr
          }
          vm[fn]()
          return vm
        }
        if (vm.$children == null || vm.$children === undefined) {
          return null
        }
        vm.$children.forEach(item => {
          return this.getChildVueComponentAttr(item, attr)
        })
      },
      importData(){
        this.importDialog = true
      },
      closeDialog(){
        this.fileList = []
      },
      delData(){
        let entityArr = []
        this.multipleSelection.forEach(item =>{
          let entity = {
            docufileId:item.docufileId,
            lcsCd:item.lcsCd
          }
          entityArr.push(entity);
        })
        if(entityArr != null && entityArr.length > 0){
          console.log(entityArr)
          this.$confirm('是否' + this.buttonNm + '该记录?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
          }).then(() => {
            axios.post('http://128.16.180.47:7111/docFile/delObject',entityArr).then(response => {
              if (response.data.resultCode == "200") {
                this.$message({
                  type: 'success',
                  message: response.data.resultMsg
                });
              }else{
                this.$message.error(response.data.resultMsg);
              }
              this.searchList();
            }).catch((error) => {
              console.log(error)
            })
          })
        }else{
          this.$message.warning('请至少选择一条数据！')
        }
      },
      recoverData(){
        let idArr = [];
        this.multipleSelection.forEach(item =>{
          idArr.push(item.docufileId);
        })
        let docufileIds = idArr.join(",")
        console.log(docufileIds)
        if(idArr.length > 0){
          this.$confirm('此操作将永久删除该资料, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
          }).then(() => {
            axios.post('http://128.16.180.47:7111/docFile/delObject',docufileIds).then(response => {
              if (response.data.resultCode == "200") {
                this.$message({
                  type: 'success',
                  message: response.data.resultMsg
                });
              }else{
                this.$message.error(response.data.resultMsg);
              }
              this.searchList();
            }).catch((error) => {
              console.log(error)
            })
          })
        }else{
          this.$message.warning('未选择要删除的资料！')
        }
      },
      reset(){
        this.projectForm = []
        this.searchList()
      }
    }
  }
</script>

<style lang="less" scoped>
  .content-cotainer {
    padding: 0 20px 20px 20px;
    height: 100%;
  }
</style>